Objective-C中的Associated Object

有时候需要在现有的对象中添加信息,我们可用通过创建一个子类,用子类创建的对象替代原有的对象。这种方式需要新建一个类,而且有时候由于一些限制,无法创建子类。Objective-C中有一项强大的特性可以解决这个问题,那就是关联对象(Associated Objective)。

###使用方式
比如我们使用 UIAlertView 的时候,用户按下按钮之后,需要用 Delegate 来处理操作,这样每一个alertView的视图创建代码和按钮处理代码就要分开,如果 alertView 很多的时候就会显得很乱。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* UIAlertView 一般使用方法
*/
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"OK", nil];
[alertView show];
//UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex ==0 ) {
NSLog(@"cancel button did press");
} else {
NSLog(@"other button did press");
}
}

如果能在 alertView 创建的时候就把按钮所对应的逻辑操作通过 Block 设置好, 然后在 Delegate 中直接调取对应 alertView 的 Block 进行操作, 这样代码就清晰很多, 我们可以通过Associated Object来给 alertView 添加临时的信息.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 用 Associated Objective 设置 button action
**/
#import <objc/runtime.h> //导入 runtime
static void *AlertViewKey = "AlertViewKey"; //Associated Object的 key
typedef void(^AlertBlock)(NSInteger);
- (void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"OK", nil];
//alertView 按钮的逻辑操作
AlertBlock alertBlock = ^(NSInteger buttonIndex){
if (buttonIndex == 0) {
NSLog(@"cancel button did press");
} else {
NSLog(@"other button did press");
}
};
//吧 alertBlock 设置成 alertView 的 Associated Object, key 为 AlertViewKey
objc_setAssociatedObject(alertView, AlertViewKey, alertBlock, OBJC_ASSOCIATION_COPY);
[alertView show];
}
//UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//获得alertView 中 key 为"AlertViewKey" 的相关信息
AlertBlock alertBlock = objc_getAssociatedObject(alertView, AlertViewKey);
alertBlock(buttonIndex);
}

相关方法

现在我们来了解一下Associated Object,

1
2
3
4
5
6
7
8
//给对象`设置`Associated Object, 每一个 Associated Object 对应一个 key
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
// 通过 key 来`获取`对象的Associated Object
id objc_getAssociatedObject(id object, const void *key)
//`移除`对象中所有的 Associated Object
void objc_removeAssociatedObjects(id object)

内存管理

objc_AssociationPolicy属性是用来Associated Object的储存策略, 对应@property 的关系如下:

objc_AssociationPolicy @property
OBJC_ASSOCIATION_ASSIGN assign
OBJC_ASSOCIATION_RETAIN_NONATOMIC nonatomatic, reatin
OBJC_ASSOCIATION_COPY_NONATOMIC nonatomic, copy
OBJC_ASSOCIATION_RETAIN retain
OBJC_ASSOCIATION_COPY copy

使用 Block 的时候要注意设置存储策略来防止Retain Cycle

还有要注意的是, key 的类型为const void *key, 所以这样设置比较好

1
static void *AlertViewKey = "AlertViewKey"; //Associated Object的 key

想要 KVO 怎么办

通过 Associated Object 添加的 property 是不会发出 KVO 的,如果想要实现的话,只要手动调用willChangeValueForKey:didChangeValueForKey:就可以了,参考一下 MJRefresh 的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static const char MJRefreshHeaderKey = '\0';
- (void)setMj_header:(MJRefreshHeader *)mj_header
{
if (mj_header != self.mj_header) {
// 删除旧的,添加新的
[self.mj_header removeFromSuperview];
[self insertSubview:mj_header atIndex:0];
// 存储新的
[self willChangeValueForKey:@"mj_header"]; // KVO
objc_setAssociatedObject(self, &MJRefreshHeaderKey,
mj_header, OBJC_ASSOCIATION_ASSIGN);
[self didChangeValueForKey:@"mj_header"]; // KVO
}
}
- (MJRefreshFooter *)mj_footer
{
return objc_getAssociatedObject(self, &MJRefreshFooterKey);
}

总结

  • 可以通过 Associated Object 把两个对象连起来.
  • 定义 Associated Object 时可以模仿@property 定义储存策略, 防止 Retain Cycle
  • 只有在其他方法不奏效的时候才使用, 因为这种方法回来带难以查找的 bug
  • 注意 KVO